// pkg_wo/pages/setPage/setPage.js

import request from '../../../utils/http.js'
const app = getApp()

// 引入SDK核心类
let QQMap = require("../../../utils/qqmap-wx-jssdk"); 
let QQMapSdk;

var EARTH_RADIUS = 6378.137; //地球半径



function rad(d) {
  return d * Math.PI / 180.0;
}
function getDistance(lng1, lat1, lng2, lat2) {
  var radLat1 = rad(lat1);
  var radLat2 = rad(lat2);
  var a = radLat1 - radLat2;
  var b = rad(lng1) - rad(lng2);
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
    + Math.cos(radLat1) * Math.cos(radLat2)
    * Math.pow(Math.sin(b / 2), 2)));
  s = s * EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  return s;//返回数值单位：公里
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selt_Id:0,
    winHeight:0,
    shopList: [],
    latitude: 0,
    longitude: 0,
    markers: [],
    count: 0,


    circles:[{
      latitude: '23.099994',
      longitude: '113.324520',
      color: '#1296db',
      fillColor: '#a5caec88',
      radius: 2000,
      strokeWidth: 2
    }],


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;


    wx.getSetting({
      success: (res) => {
          // console.log("设置信息"+JSON.stringify(res))
          if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
              wx.showModal({
                  title: '请求授权当前位置',
                  content: '需要获取您的地理位置，请确认授权',
                  success: function (res) {
                      if (res.cancel) {
                          wx.showToast({
                              title: '拒绝授权',
                              icon: 'none',
                              duration: 1000
                          })
                      } else if (res.confirm) {
                          wx.openSetting({
                              success: function (dataAu) {
                                  if (dataAu.authSetting["scope.userLocation"] == true) {
                                      wx.showToast({
                                          title: '授权成功',
                                          icon: 'success',
                                          duration: 1000
                                      })
                                      //再次授权，调用wx.getLocation的API
                                      that.getLocation();
                                  } else {
                                      wx.showToast({
                                          title: '授权失败',
                                          icon: 'none',
                                          duration: 1000
                                      })
                                  }
                              }
                          })
                      }
                  }
              })
          } else if (res.authSetting['scope.userLocation'] == undefined) {
              //调用wx.getLocation的API
              that.getLocation();
          } else {
              //调用wx.getLocation的API
              that.getLocation();
          }
      }
  })



 
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight - (670 / 750 * res.windowWidth),
        });
      }
    });



  },



  // // 获取位置的方法
  getLocation() {
    // 1.先开始定位
  
    var that=this
    wx.getLocation({
      type: 'gcj02',
      success (res) {

        // console.log("地图地址:" + JSON.stringify(res))
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          "circles[0].longitude": res.longitude,
          "circles[0].latitude": res.latitude
        })


        that.getShopList();

        // that.getCenterLocation()
      }
     })
  },





  //获取关联店铺List
  getShopList: function () {
    var that = this
    // console.log("that.data.personId:"+that.data.personId)
    let data = {
      jingDu: that.data.longitude,
      weiDu: that.data.latitude,
      distance: 2000,
    }

    request.get('dianPu/searchDianPu', data, res => {
      // console.log("店铺List:" + JSON.stringify(res.data))

      if(res.data.status!=404 && res.data.status!=500){
      that.setData({
        shopList: res.data,
        markers: [],
      })


      if (Array.isArray(that.data.shopList) && that.data.shopList.length !== 0) {
        that.setData({
          count: that.data.shopList.length
        })

        that.data.shopList.forEach((item, index) => {
          var pointObj = {}
            pointObj.id = item.dianPuId
            pointObj.latitude = item.mapWeiDu,
            pointObj.longitude = item.mapJingDu,
            pointObj.width = 40, //图片显示宽度
            pointObj.height = 40, //图片显示高度
            pointObj.iconPath = '/pkg_wo/image/shopAddress.png',
            pointObj.title = item.mingCheng

          // if (that.data.dianPuId == item.dianPuId) {
          //   pointObj.iconPath = '/pkg_wo/image/shopAddress.png'
          //   that.setData({
          //     latitude: item.mapWeiDu,
          //     longitude: item.mapJingDu,
          //   })
          // } else {
          //   pointObj.iconPath = '/pkg_wo/image/shopAddressNo.png'
          // }


          that.data.markers.push(pointObj)
        })


        that.setData({
          markers: that.data.markers
        })

   

      }
    }

      wx.setNavigationBarTitle({
        title: '附近服务站:' + that.data.count + '个'
      })

    }, err => {
      wx.showToast({
        title: '获取店铺失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })
  },

 

  changeDianPu: function (e) {
    var index = e.currentTarget.dataset.index;

    var that=this

    that.setData({
      latitude:  that.data.markers[index].latitude,
      longitude:  that.data.markers[index].longitude,
      selt_Id: that.data.shopList[index].dianPuId
    })


    that.data.markers.forEach((item, idex) => {
      // console.log(item.id+'==='+id)
      if (idex==index) {
        item.iconPath = '/pkg_wo/image/shopAddress.png'
      } else {        
          item.iconPath = '/pkg_wo/image/shopAddressNo.png'
      }

    });

    that.setData({
      markers:that.data.markers
    })

  


    // wx.navigateBack({
    //   delta: 1,
    // })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})